package com.situ.day10;

import com.situ.day9.Student;
import org.junit.Test;

import java.util.*;

public class CollectionDemo {
    @Test
    public void test1() {
        String[] array = new String[3];
        //List：有序可重复
        //有序：放到集合里面的顺序和拿出来顺序一致
        //ArrayList<String> list = new ArrayList<>();
        List<String> list = new ArrayList<>();
        list.add("Java");
        list.add("UI");
        list.add("H5");
        list.add("H5");
        list.add("aa");
        for (String str : list) {
            System.out.println(str);
        }
        System.out.println("-----------------");
        //Set:无序不重复
        //无序：放到集合里面的顺序和拿出来顺序可能不一致
        //HashSet<String> set = new HashSet<>();
        Set<String> set = new HashSet<>();
        set.add("Java");
        set.add("UI");
        set.add("H5");
        set.add("H5");
        set.add("aa");
        for (String str : set) {
            System.out.println(str);
        }

    }

    @Test
    public void test3() {
        Map<String, String> map =  new HashMap<>();
        map.put("cn", "中国");
        map.put("us", "美国");
        map.put("uk", "英国");
        Set<Map.Entry<String, String>> set = map.entrySet();
        for (Map.Entry<String, String> entry : set) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }
        System.out.println("----------");
        String country = map.get("cn");
        System.out.println(country);
        System.out.println("----------");
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            System.out.println(key + " : " + map.get(key));
        }

    }

    @Test
    public void test13() {
        //Set遍历的时候，可以使用增强的for循环，也可以使用迭代器遍历
        HashMap<String, String> map = new HashMap<>();
        map.put("cn", "中国");
        map.put("us", "美国");
        map.put("uk", "英国");
        Set<String> keySet = map.keySet();
        Iterator<String> iterator1 = keySet.iterator();
        while (iterator1.hasNext()) {
            String key = iterator1.next();
            System.out.println(key + " : " + map.get(key));
        }
        System.out.println("-----------------");
        Set<Map.Entry<String, String>> entrySet = map.entrySet();
        Iterator<Map.Entry<String, String>> iterator2 = entrySet.iterator();
        while (iterator2.hasNext()) {
            Map.Entry<String, String> entry = iterator2.next();
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }
    }

}
